#include "lua_wraper.h"

void LuaDelegate::Stop()
{

}

int LuaDelegate::DoString(QString scr)
{
    if (mVM != nullptr){
        int ret = luaL_dostring(mVM,scr.toStdString().c_str());
        if (ret > 0){
            printf("lua error");
            PrintError(mVM);
            return -1;
        }
    }
    return 0;
}

int LuaDelegate::UpdateScript(QString scr)
{
    if(nullptr != mVM){
        lua_close(mVM);
    }
    free(mVM);
    mVM = luaL_newstate();		//打开lua
    if(nullptr != mVM){
        printf("shit is nullptr");
    }
    luaL_openlibs(mVM);					//打开标准库
    lua_register(mVM, "showbuffer", LuaShowData);
    lua_register(mVM, "serial_send", LuaWriteUart);

    if (mVM != nullptr){
        int ret = luaL_dostring(mVM,scr.toStdString().c_str());
        if (ret > 0){
            printf("lua error");
            PrintError(mVM);
            return -1;
        }
    }
    return 0;
}

void LuaDelegate::PrintError(lua_State *L)
{
    qDebug()<<"\nFATAL ERROR:%s\n\n"<< lua_tostring(L, -1);
}

void LuaDelegate::OnSerialData(QString data){
    int i = lua_getglobal(mVM,"OnDataReady");
    if(i == 0)
        return;
    lua_pushstring(mVM,data.toStdString().data());
    lua_call(mVM,1,0);
}

void LuaDelegate::OnNetworkData(char *addr, char *data, uint32_t port)
{
    qDebug()<<"call lua network callback";
    int ret = lua_getglobal(mVM,"OnNetworkData");
    if(ret == 0)
        return;
    qDebug()<<ret;
    lua_pushstring(mVM,addr);
    lua_pushstring(mVM,data);
    lua_pushnumber(mVM,port);
    lua_call(mVM,3,0);
}

void LuaDelegate::OnNewTcpClient(QString ip, uint32_t port, uint32_t sockptr)
{
    qDebug()<<"call lua network new client callback";
    int ret = lua_getglobal(mVM,"OnNewClient");
    if(ret == 0)
        return;
    qDebug()<<ret;
    lua_pushstring(mVM,ip.toStdString().c_str());
    lua_pushinteger(mVM,port);
    lua_pushinteger(mVM,sockptr);
    lua_call(mVM,3,0);
}

void LuaDelegate::OnClientLeave(QString ip, uint32_t port, uint32_t sockptr)
{
    qDebug()<<"call lua network leave callback";
    int ret = lua_getglobal(mVM,"OnClientLeave");
    if(ret == 0)
        return;
    qDebug()<<ret;
    lua_pushstring(mVM,ip.toStdString().c_str());
    lua_pushnumber(mVM,port);
    lua_pushnumber(mVM,sockptr);
    lua_call(mVM,3,0);
}


void LuaDelegate::DumpStack()
{
    static int count = 0;
    printf("begin dump lua stack:%d\n", count);
    int top = lua_gettop(mVM);
    for (int i = top; i > 0; --i)
    {
        int t = lua_type(mVM, i);
        switch (t)
        {
        case LUA_TSTRING:
            qDebug()<<("%s\n", lua_tostring(mVM, i));
            break;
        case LUA_TBOOLEAN:
            qDebug()<<(lua_toboolean(mVM, i) ? "true\n" : "false\n");
            break;
        case LUA_TNUMBER:
            qDebug()<<("%g\n", lua_tonumber(mVM, i));
            break;
        default:
            qDebug()<<("%s\n", lua_typename(mVM, t));
            break;
        }
    }
    ++count;
}


LuaDelegate::~LuaDelegate()
{
    if(nullptr != mVM){
        lua_close(mVM);
    }
}

LuaDelegate::LuaDelegate():
    mVM(nullptr)
{
    mVM = luaL_newstate();		//打开lua
    if(nullptr != mVM){
        printf("shit is nullptr");
    }
    luaL_openlibs(mVM);					//打开标准库
    lua_register(mVM, "showbuffer", LuaShowData);
    lua_register(mVM, "serial_send", LuaWriteUart);

}

int LuaDelegate::DoFile(QString path)
{
    if(mVM != nullptr){

        int ret = luaL_dofile(mVM, path.toStdString().c_str());
        if (ret > 0){
            printf("lua error");
            return -1;
        }
    }
    return 0;
}

